//
//  VisitorView.swift
//  swift_wobo
//
//  Created by yuxin on 2018/11/15.
//  Copyright © 2018 wuxiantianli. All rights reserved.
//

import UIKit

protocol VisitorViewDelegate : NSObjectProtocol {
    func visitorViewLogin()
    func visitorViewRegister()
}

class VisitorView: UIView {
    
    weak var delegate: VisitorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        backgroundColor = UIColor.white
        addSubview(bgView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
    
    }
    
    func setupVisitorInfo(isHome: Bool, imageName: String, message: String) {
        bgView.isHidden = !isHome
        messageLabel.text = message
        iconView.image = UIImage(named: imageName)
        
        if isHome {
            startAnimation()
        }
        layoutIfNeeded()
    }
    
    // MARK: - 内部控制方法
    
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        bgView.layer.add(anim, forKey: nil)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        bgView.center = center
        maskIconView.frame = bounds
        iconView.center = center
        messageLabel.sizeToFit()
        let spacing : CGFloat = 30.0
        
         let limitBounds:CGRect=CGRect(origin:CGPoint(x:100,y:100),size:CGSize(width:CGFloat(UIScreen.main.bounds.width-spacing*2),height:100))
    
        let msgSize = messageLabel.textRect(forBounds: limitBounds , limitedToNumberOfLines: 0)
        
        let frame : CGRect = CGRect(x: spacing, y:iconView.frame.maxY + 40 , width: limitBounds.width, height: msgSize.height)
        
        messageLabel.frame = frame
        
        let registerBtnX = (bounds.width - spacing)/2.0 - 100.0
        registerBtn.frame = CGRect(x: registerBtnX, y: messageLabel.frame.maxY+20.0, width: 100.0, height: 35.0)
        loginBtn.frame = CGRect(origin: CGPoint(x: registerBtn.frame.maxX + spacing, y: registerBtn.frame.minY), size: registerBtn.frame.size)
        
    }
    
    
    @objc func registerBtnClick() {
        delegate?.visitorViewRegister()
    }
    
    @objc func loginBtnClick() {
        delegate?.visitorViewLogin()
    }
    
    //MARK: - 懒加载
    private lazy var bgView : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    private lazy var maskIconView : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()
    
    private lazy var iconView : UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    
    private lazy var messageLabel : UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14.0)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        
        return label
    }()
    
    private lazy var registerBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", for: .normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.addTarget(self, action: #selector(registerBtnClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var loginBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", for: UIControl.State.normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        return btn
    }()
    
}
